app.controller('QRCtrl', function ($scope, $modalInstance, $modal, toaster, param, ajax, $rootScope) {

	$scope.submitting = false;
	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			$scope.submitting = true;
			inserter.fields = {
				"userId": $rootScope.loginUser.id,
				"start": $scope.param.data.id,
				"terminal": $scope.terminalName
			};
			inserter.resource = 'starting/bk/qr';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '二维码生成成功。');
					$modal.open({
						templateUrl: 'template/booking/start/showQR.html?v=58fb3bc51e',
						controller: 'ModalCtrl',
						size: 'lg',
						resolve: {
							param: function () {
								return {
									"start": $scope.param.data.name,
									"terminal": $scope.terminalName,
									"url": result.result.url,
									"qr": result.result.qr
								};
							}
						}
					});
					$modalInstance.dismiss('cancel');
				} else {
					toaster.pop('error', '错误', result.result);
					$scope.submitting = false;
				}
			}, function () {
				toaster.pop('error', '错误', '生成二维码失败。');
				$scope.submitting = false;
			});
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
		$scope.submitting = false;
	};
});